package design.pattern.Lu19VisitorPattern;

import java.util.Random;

/**
 * @program JavaBase
 * @description: 访问者模式 通用的模版
 *  一个模版被多个访问者访问
 * @author: zhanglu
 * @create: 2020-05-07 16:53:00
 */
public class VisitorPattern03 {

    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            Element03 element = ObjectStruture03.createElement();
            element.accept(new Visitor03());
        }
    }
}

abstract class Element03{
    abstract void doSomething();
    abstract void accept(IVisit03 iVisit03);
}

class ConcreteElement01 extends Element03{
    @Override
    void doSomething() {
        System.out.println("访问者1...");
    }
    @Override
    void accept(IVisit03 iVisit03) {
        iVisit03.visit(this);
    }
}
class ConcreteElement02 extends Element03{
    @Override
    void doSomething() {
        System.out.println("访问者2...");
    }
    @Override
    void accept(IVisit03 iVisit03) {
        iVisit03.visit(this);
    }
}

interface IVisit03{
    <T> void visit(T t);
}

class Visitor03 implements IVisit03{

    @Override
    public <T> void visit(T t) {
        Element03 element03 = (Element03) t;
        element03.doSomething();
    }
}

class ObjectStruture03{
    public static Element03 createElement(){
        Random random = new Random();
        if(random.nextInt(100) > 50){
            return new ConcreteElement01();
        }else {
            return new ConcreteElement02();
        }
    }
}
